package com.glorysoft.tonglingxjpda.ui.setting.view

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.View
import com.glorysoft.tonglingxjpda.ui.setting.viewmodel.SettingViewModel
import com.glorysoft.tonglingxjpda.BR
import com.glorysoft.tonglingxjpda.R
import com.glorysoft.tonglingxjpda.base.BaseActivity
import com.glorysoft.tonglingxjpda.databinding.ActivitySettingBinding
import com.glorysoft.tonglingxjpda.entity.ui.ToastMessageBean
import com.glorysoft.tonglingxjpda.utils.CacheDataManger
import com.glorysoft.tonglingxjpda.utils.FileUtils
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils
import kotlinx.android.synthetic.main.activity_setting.*
import java.io.File

/**
 * FileName: SettingActivity
 * Author: XJH
 * Date: 2021/5/13 14:40
 * Description:
 * History:
 */
class SettingActivity : BaseActivity<SettingViewModel, ActivitySettingBinding>(), View.OnClickListener {

    var confirmAndCancelDialog :  FreeUI_GeneralFragmentDialog? = null

    /**
     * 创建Handler
     * 接收消息
     */
    @SuppressLint("HandlerLeak")
    val handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            dismissLoading()
            if (msg.what == 0){
                viewModel.defUI.toastEvent.value = ToastMessageBean("缓存清除成功",true)
                try {
                    tvCache.text = CacheDataManger.getTotalCacheSize(this@SettingActivity)
                }catch (e : Exception){
                    e.printStackTrace()
                }
            }else if(msg.what == 1){
                viewModel.defUI.toastEvent.value = ToastMessageBean("数据清除成功",true)
                try {
                    tvData.text = CacheDataManger.getFileOrFilesSize(FileUtils.getDownloadDataPath(this@SettingActivity))
                }catch (e : Exception){
                    e.printStackTrace()
                }
            }else{
                viewModel.defUI.toastEvent.value = ToastMessageBean("清除失败",false)
            }
            super.handleMessage(msg)
        }
    }


    override fun layoutId(): Int {
        return R.layout.activity_setting
    }

    override fun initVariableId(): Int {
        return BR.settingViewModel
    }

    override fun initData() {
        rlIP.setOnClickListener(this)
        rlFileIP.setOnClickListener(this)
        llCacheLayout.setOnClickListener(this)
        llDataLayout.setOnClickListener(this)


    }

    override fun initView(savedInstanceState: Bundle?) {
        viewModel.initVersion(this)
        try {
            tvCache.text = CacheDataManger.getTotalCacheSize(this)
            tvData.text = CacheDataManger.getFileOrFilesSize(FileUtils.getDownloadDataPath(this@SettingActivity))
        }catch (e : Exception){
            e.printStackTrace()
        }

    }

    override fun onClick(p0: View?) {
        when(p0?.id){
            R.id.rlIP -> viewModel.showScanDialog(this,0)
            R.id.rlFileIP -> viewModel.showScanDialog(this,1)
            R.id.llCacheLayout ->{

                confirmAndCancelDialog = FreeApi_DialogUtils.getConfirmAndCancelDialog(
                    this,
                    "此操作会导致之前缓存数据全部清除，是否确定？",
                    object : FreeUI_CommonCallback {
                        override fun onSuccess() {
                            showLoading()
                            Thread(ClearCache()).start()
                        }

                    },
                    object : FreeUI_CommonCallback {
                        override fun onSuccess() {
                            confirmAndCancelDialog?.dismiss()
                        }

                    })

                confirmAndCancelDialog?.show(supportFragmentManager,"111")
            }
            R.id.llDataLayout ->{
                showLoading()
                Thread(ClearData()).start()
            }
        }
    }

    inner class ClearCache() : Runnable{

        override fun run() {
            try {
                CacheDataManger.clearAllCache(this@SettingActivity)
                Thread.sleep(1000)

                if (CacheDataManger.getTotalCacheSize(this@SettingActivity).startsWith("0")) {
                    handler.sendEmptyMessage(0)
                }else{
                    handler.sendEmptyMessage(2)
                }


            } catch (e : Exception) {
                e.printStackTrace()
                viewModel.defUI.toastEvent.value = ToastMessageBean("清除失败",false)
                return
            }
        }

    }

    inner class ClearData() : Runnable{

        override fun run() {
            try {

                CacheDataManger.deleteDirFile(File(FileUtils.getDownloadDataPath(this@SettingActivity)))
                Thread.sleep(1000)

                if (CacheDataManger.getFileOrFilesSize(FileUtils.getDownloadDataPath(this@SettingActivity)).startsWith("0")){
                    handler.sendEmptyMessage(1)
                }else{
                    handler.sendEmptyMessage(2)
                }
            } catch (e : Exception) {
                e.printStackTrace()
                viewModel.defUI.toastEvent.value = ToastMessageBean("清除失败",false)
                return
            }
        }

    }
}